<template>
	<view>
		<view class="input_box">
			<view class="input_" @click="show=true">
				<view style="height: 80rpx;line-height: 80rpx;">代理类型</view>
				<view class="icon_img1">
					<view class="">{{huZhuName}}</view>
					<u-icon name="arrow-right" size="20"></u-icon>
				</view>
			</view>
			<view class="input_">
				<view style="height: 80rpx;line-height: 80rpx;">申请人</view>
				<input type="text" v-model="formDate.name" placeholder="请输入申请人名称" class="input" />
			</view>
			<view class="input_">
				<view style="height: 80rpx;line-height: 80rpx;">申请人电话</view>
				<input type="text" v-model="formDate.phone" placeholder="请输入申请人电话" class="input" />
			</view>
			<!-- <view class="input_">
				<view style="height: 80rpx;line-height: 80rpx;">紧急联系人</view>
				<input type="text" v-model="formDate.emergencyContacts" placeholder="请输入紧急联系人名称" class="input" />
			</view>
			<view class="input_">
				<view style="height: 80rpx;line-height: 80rpx;">紧急联系人电话</view>
				<input type="text" v-model="formDate.emergencyCalls" placeholder="请输入紧急联系人电话" class="input" />
			</view> -->
			<view class="input_" v-if="formDate.type==2" @click="locationClick(1)">
				<view style="height: 80rpx;line-height: 80rpx;">详细地址</view>
				<input type="text" v-model="formDate.address" @focus="locationClick(0)" placeholder="请输入详细地址"
					class="input" />
			</view>
			<view class="input_" v-if="formDate.type==2">
				<view style="height: 80rpx;line-height: 80rpx;">小区名字</view>
				<input type="text" v-model="formDate.community" placeholder="请输入小区名字" class="input" />
			</view>




		</view>
		<view class="btn_fix">
			<view class="" style="margin-bottom: 20rpx;">

				<u-checkbox-group @change="checkboxGroupChange">
					<u-checkbox @change="checkboxChange" v-model="item.checked" v-for="(item, index) in list2"
						:key="index" :name="item.name"><span>同意</span>
						<span style="color: #2869FE;" @click="tourl(0)" v-if="formDate.type==0">《推广员协议》</span>
						<span style="color: #2869FE;" @click="tourl(1)" v-if="formDate.type==1">《区域代理协议》</span>
						<span style="color: #2869FE;" @click="tourl(2)"
							v-if="formDate.type==2">《小区代理协议》</span></u-checkbox>
				</u-checkbox-group>
			</view>

			<view class="btn" @click="submit">提交</view>
		</view>
		<u-select v-model="show" :list="list" @close='show=false' @confirm='confirm'></u-select>
	</view>
</template>

<script>
	import {
		auth
	} from '@/common/authorize.js'; //权限判断
	export default {
		data() {
			return {

				list2: [{
					name: 'apple',
					checked: false,
					disabled: false
				}],
				// u-radio-group的v-model绑定的值如果设置为某个radio的name，就会被默认选中
				value: '',
				show: false,
				formDate: {
					name: '', //申请人姓名"
					phone: '', //申请人电话
					emergencyContacts: '', //紧急联系人姓名
					emergencyCalls: '', //紧急联系人电话
					community: '', //小区名字
					address: '', //详细地址
					type: 0, //类型
					longitude: '',
					latitude: ''
				},
				list: [{
					label: '推广员',
					value: 0
				}, {
					label: '区域代理',
					value: 1
				}, {
					label: '小区代理',
					value: 2
				}],
				huZhuName: '推广员',
				xieyi: false
			}
		},
		onLoad() {},
		methods: {
			tourl(e) {
				uni.navigateTo({
					url: '/pages/xieyi/xieyi?type=' + e
				})
			},
			// 选中某个复选框时，由checkbox时触发
			checkboxChange(e) {
				this.xieyi = e.value
			},
			// 选中任一checkbox时，由checkbox-group触发
			checkboxGroupChange(e) {
				// console.log(e);
			},
			// 全选
			checkedAll() {
				this.list.map(val => {
					val.checked = true;
				})
			},

			async locationClick(e) { //选择服务地址
				//console.log("选地址");//微信开发者工具点了可能没有反应
				console.log(e)
				uni.chooseLocation({
					success: (res) => {
						console.log("选择", res)
						this.formDate.longitude = res.longitude
						this.formDate.latitude = res.latitude
						this.formDate.address = res.address + res.name
					}
				})
			},
			confirm(e) {
				console.log(e, 'e')
				this.formDate.type = e[0].value
				this.huZhuName = e[0].label
				this.show = false
			},
			tobuy(e) {
				let that = this
				this.$request('pay/xiaoChengXuProxyPay', {
					id: e
				}).then(res2 => {
					if (res2.msg == '操作成功') {
						let mood = res2.data

						// #ifdef MP-WEIXIN
						uni.requestPayment({
							provider: 'wxpay', // 服务提提供商
							timeStamp: mood.timeStamp, // 时间戳
							nonceStr: mood.nonceStr, // 随机字符串
							package: mood.package,
							signType: mood.signType, // 签名算法
							paySign: mood.paySign, // 签名
							success: function(res3) {
								uni.showToast({
									title: '支付成功',
									icon: 'none'
								})
							},
							fail: function(err) {
								console.log('支付失败', err);
							}
						});
						//#endif

						//#ifdef H5
						if (that.$jwx && that.$jwx.isWechat()) { //检查是否是微信环境
							that.$jwx.wxpay({ //调用支付，
								'timestamp': mood.timeStamp,
								'nonceStr': mood.nonceStr,
								'package': mood.package,
								'signType': mood.signType,
								'paysign': mood.paySign
							}, function(r) {
								if (r.errMsg == 'chooseWXPay:ok') {
									uni.showToast({
										title: '支付成功',
										icon: 'none'
									})
								} else {
									that.$msg('支付失败！');
								}
							});

						} else {
							uni.showToast({
								title: '请在微信环境下运行',
								icon: 'none'
							})
							return
						}
						//#endif



					}
				})
			},
			async submit() {
				if (!this.xieyi) {
					uni.showToast({
						title: '请同意协议后提交',
						icon: 'none'
					})
					return false
				}
				let data = {}
				if (this.formDate.type === 0) {
					data = {
						name: this.formDate.name, //申请人姓名"
						phone: this.formDate.phone, //申请人电话
						// emergencyContacts: this.formDate.emergencyContacts, //紧急联系人姓名
						// emergencyCalls: this.formDate.emergencyCalls, //紧急联系人电话
						type: this.formDate.type, //类型
					}
				} else if (this.formDate.type === 2) {
					data = {
						name: this.formDate.name, //申请人姓名"
						phone: this.formDate.phone, //申请人电话
						// emergencyContacts: this.formDate.emergencyContacts, //紧急联系人姓名
						// emergencyCalls: this.formDate.emergencyCalls, //紧急联系人电话
						community: this.formDate.community, //小区名字
						address: this.formDate.address, //详细地址
						type: this.formDate.type, //类型
						longitude: this.formDate.longitude,
						latitude: this.formDate.latitude
					}

				} else {
					data = {
						name: this.formDate.name, //申请人姓名"
						phone: this.formDate.phone, //申请人电话
						// emergencyContacts: this.formDate.emergencyContacts, //紧急联系人姓名
						// emergencyCalls: this.formDate.emergencyCalls, //紧急联系人电话
						type: this.formDate.type, //类型
					}
				}

				for (let k in data) {
					if (data[k] === '') {
						uni.showToast({
							title: '请填写完整信息',
							icon: 'none'
						})
						return false
					}
				}
				let res = await this.$request("kehu/jiajiaProxyApplication/add", this.formDate, 'post')
				if (res.msg == "操作成功") {
					if (this.formDate.type === 0 || this.formDate.type == 2) {
						this.tobuy(res.data)
					} else {
						uni.showToast({
							title: '申请成功',
							icon: 'none'
						})
					}
				}
			},

		}
	}
</script>

<style scoped lang="scss">
	.infoItem {
		width: 710rpx;
		background: #FFFFFF;
		border-radius: 10rpx;
		margin-bottom: 40rpx;
		padding: 32rpx 30rpx;
		box-sizing: border-box;

		.textAreaBox {
			margin-top: 15rpx;
		}

		.title {
			font-family: PingFang SC;
			font-weight: 400;
			font-size: 36rpx;
			color: #333333;
		}

		.textPic {
			font-weight: 400;
			font-size: 30rpx;
			color: #B8B7B7;
			margin-top: 36rpx;
			margin-bottom: 43rpx;
		}

		.form {
			box-sizing: border-box;
			margin-top: 20rpx;
			margin-bottom: 19rpx;
			width: 100%;
			background: #FFFFFF;
			border-radius: 10rpx;
		}
	}

	.btn {
		width: 686rpx;
		height: 80rpx;
		text-align: center;
		line-height: 80rpx;
		color: #fff;
		background-color: #2869FE;
		border-radius: 8rpx;
	}

	.btn_fix {
		position: fixed;
		bottom: 5%;
		left: 32rpx;
	}

	.icon_img1 {
		display: flex;
		align-items: center;
	}

	.input {
		width: 400rpx;
		height: 80rpx;
		text-align: right;
	}

	.input_box {
		width: 710rpx;
		padding: 20rpx 30rpx;
		background-color: #fff;
		margin-left: 20rpx;
		margin-top: 30rpx;
		border-radius: 10rpx;
		box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
	}

	.input_ {
		display: flex;
		align-items: center;
		font-size: 28rpx;
		justify-content: space-between;
		margin-top: 25rpx;
	}
</style>